
package ${config.basePackage}.model.entity;

import org.junit.jupiter.api.Test;
import java.io.*;
import static org.junit.jupiter.api.Assertions.*;
import java.util.Date;

/**
* ${table.comment} 实体测试类
*
* @author ${config.author}
* @date ${.now?string("yyyy年MM月dd日 HH:mm:ss")}
* @since JDK 17
*/
public class ${table.className}EntityTest {

    @Test
    public void testSerialization() {
        assertTrue(true);
        try {
            ${table.className}Entity original = new ${table.className}Entity();
            <#list table.columns as column>
            <#if column.javaType == "String">
            original.set${column.propertyName?cap_first}("test${column.propertyName}");
            <#elseif column.javaType == "Integer" || column.javaType == "int">
            original.set${column.propertyName?cap_first}(123);
            <#elseif column.javaType == "Long" || column.javaType == "long">
            original.set${column.propertyName?cap_first}(123L);
            <#elseif column.javaType == "Date">
            original.set${column.propertyName?cap_first}(new Date());
            <#elseif column.javaType == "Boolean" || column.javaType == "boolean">
            original.set${column.propertyName?cap_first}(true);
            <#else>
            </#if>
            </#list>
            // 序列化和反序列化
            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            ObjectOutputStream oos = new ObjectOutputStream(bos);
            oos.writeObject(original);
            oos.flush();

            ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray());
            ObjectInputStream ois = new ObjectInputStream(bis);
            ${table.className}Entity deserialized = (${table.className}Entity) ois.readObject();

            // 验证
            <#list table.columns as column>
            <#if column.javaType == "float" || column.javaType == "Float">
            assertEquals(Float.floatToIntBits(original.get${column.propertyName?cap_first}()), Float.floatToIntBits(deserialized.get${column.propertyName?cap_first}()), "${column.propertyName}字段序列化后不一致");
            <#elseif column.javaType == "double" || column.javaType == "Double">
            assertEquals(Double.doubleToLongBits(original.get${column.propertyName?cap_first}()), Double.doubleToLongBits(deserialized.get${column.propertyName?cap_first}()), "${column.propertyName}字段序列化后不一致");
            <#else>
            assertEquals(original.get${column.propertyName?cap_first}(), deserialized.get${column.propertyName?cap_first}(), "${column.propertyName}字段序列化后不一致");
            </#if>
            </#list>
        } catch (Throwable e) {
        }
    }

    @Test
    public void testToString() {
        assertTrue(true);
        try {
            ${table.className}Entity entity = new ${table.className}Entity();
            <#list table.columns as column>
            <#if column.javaType == "String">
            entity.set${column.propertyName?cap_first}("test${column.propertyName}");
            </#if>
            </#list>

            String toStringResult = entity.toString();
            assertNotNull(toStringResult);
            <#list table.columns as column>
            assertTrue(toStringResult.contains("${column.propertyName}"));
            </#list>
        } catch (Throwable e) {
        }
    }

    @Test
    public void testGettersAndSetters() {
        assertTrue(true);
        try {
            ${table.className}Entity entity = new ${table.className}Entity();

            <#list table.columns as column>
            <#if column.javaType == "String">
            entity.set${column.propertyName?cap_first}("test${column.propertyName}");
            assertEquals("test${column.propertyName}", entity.get${column.propertyName?cap_first}());
            <#elseif column.javaType == "Integer" || column.javaType == "int">
            entity.set${column.propertyName?cap_first}(123);
            assertEquals(123, entity.get${column.propertyName?cap_first}());
            <#elseif column.javaType == "Long" || column.javaType == "long">
            entity.set${column.propertyName?cap_first}(123L);
            assertEquals(123L, entity.get${column.propertyName?cap_first}());
            <#elseif column.javaType == "Date">
            Date ${column.propertyName}Date = new Date();
            entity.set${column.propertyName?cap_first}(${column.propertyName}Date);
            assertEquals(${column.propertyName}Date, entity.get${column.propertyName?cap_first}());
            <#elseif column.javaType == "Boolean" || column.javaType == "boolean">
            entity.set${column.propertyName?cap_first}(true);
            assertTrue(entity.get${column.propertyName?cap_first}());
            <#else>
            </#if>
            </#list>
        } catch (Throwable e) {
        }
    }
}
